<?php
class NewsController extends AppController {

    public $helpers = array('Time','Rss','Form','Pagination');

    public function admin_index() {
        $this->News->recursive = 0;
        $this->paginate = array('order' => 'News.created desc');
        $this->set('news', $this->paginate());
    }

    public function admin_add() {
        if (!empty($this->data)) {
            $this->News->create();
            if ($this->News->save($this->data)) {
                $this->Session->setFlash(__('The News have been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The News could not be saved. Please, try again.',true));
            }
        }
        $this->set('users',$this->News->User->find('list'));
    }

    public function admin_edit($id = null) {
        if (!$this->News->findById($id)) {
            $this->Session->setFlash(__('Invalid news',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if (!empty($this->data)) {
            if ($this->News->save($this->data)) {
                $this->Session->setFlash(__('The News have been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The News could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->News->read(null, $id);
        }
        $this->set('users',$this->News->User->find('list'));
    }

    public function admin_delete($id = null) {
        if (!$this->News->findById($id)) {
            $this->Session->setFlash(__('Invalid news',true));
        }
        if ($this->News->del($id)) {
            $this->Session->setFlash(__('News deleted',true));
        } else {
            $this->Session->setFlash(__('The News could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_preview() {
        $this->set('preview',$this->data['News']);
    }

    public function admin_delete_news() {
        if($this->News->deleteAll(array('News.id'=>$this->data['NewsDelete']))) {
            $this->Session->setFlash(__('News deleted',true));
        } else {
            $this->Session->setFlash(__('The News could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function index() {
        $this->News->recursive = 0;
        $this->paginate = array('limit'=>5,'order' => 'News.created desc');
        $this->set('news', $this->paginate());
    }

    public function rss() {
        $this->set('news',$this->News->findAll(null,null,array('News.created'=>'desc')));
    }

    public function view($seo_link = null) {
        $created = '%'.$this->params['year'] . '-' . $this->params['month'] . '-' . $this->params['day'] . '%';
        $this->set("news", $this->News->find(array('News.seo_link' => $seo_link,'News.created like "'.$created.'"')));
    }

    public function sidebox() {
        return $this->News->last();
    }
}
?>